<template>
    <div>
        <div class="crumbs">
            <el-breadcrumb separator="/">
                <el-breadcrumb-item> <i class="el-icon-lx-cascades"></i> 公司管理 </el-breadcrumb-item>
            </el-breadcrumb>
        </div>
        <div class="container">
            <div class="handle-box">
                <el-input v-model="query.name" placeholder="公司名称" class="handle-input mr10" @change="handleSearch"></el-input>
                <el-button type="primary" icon="el-icon-search" @click="handleSearch">搜索</el-button>
                <div class="div-add" style="display: block">
                    <el-button style="margin: 10px 0px" type="primary" @click="addCompany">+ 新增公司</el-button>
                    <el-button type="primary" @click="addBaseTable">+ 新增软文</el-button>
                </div>
            </div>
            <el-table
                :data="tableData"
                border
                class="table"
                ref="multipleTable"
                header-cell-class-name="table-header"
                @selection-change="handleSelectionChange"
            >
                <el-table-column type="index" label="序号" width="70" align="center"> </el-table-column>
                <!-- <el-table-column type="selection" width="55" align="center"></el-table-column> -->
                <!-- <el-table-column prop="id" label="ID" width="55" align="center"></el-table-column> -->
                <el-table-column prop="companyName" label="公司名称" align="center" width="180"></el-table-column>
                <el-table-column label="LOGO图" width="150" align="center">
                    <template slot-scope="scope">
                        <el-image
                            class="table-td-thumb"
                            :src="scope.row.companyLogo"
                            :preview-src-list="[scope.row.companyLogo]"
                        ></el-image>
                    </template>
                </el-table-column>
                <el-table-column prop="companyIntroduce" label="公司二维码" width="300" align="center"></el-table-column>
                <el-table-column prop="productCount" label="产品数" width="80" align="center"></el-table-column>
                <!-- <el-table-column prop="endTime" label="闭展时间" width="150"></el-table-column> -->
                <!-- <el-table-column label="账户余额">
                    <template slot-scope="scope">￥{{scope.row.money}}</template>
                </el-table-column> -->

                <!-- <el-table-column prop="exhibitStatus" label="当前状态" width="250"></el-table-column> -->
                <el-table-column prop="createTime" label="发布时间" width="180" align="center"></el-table-column>
                <!-- <el-table-column label="状态" align="center">
                    <template slot-scope="scope">
                        <el-tag
                            :type="scope.row.state==='成功'?'success':(scope.row.state==='失败'?'danger':'')"
                        >{{scope.row.state}}</el-tag>
                    </template>
                </el-table-column> -->

                <!-- <el-table-column prop="date" label="注册时间"></el-table-column> -->
                <el-table-column label="操作" align="center" width="300">
                    <template slot-scope="scope">
                        <el-button type="info" @click="toDetail(scope.$index, scope.row)">产品管理</el-button>
                        <el-button type="primary" @click="toCheck(scope.$index, scope.row.companyId)">查看</el-button>
                        <el-button type="success" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
                        <el-button type="danger" @click="handleDelete(scope.$index, scope.row.companyId)">删除</el-button>
                    </template>
                </el-table-column>
            </el-table>
            <div class="pagination">
                <el-pagination
                    background
                    layout="total, prev, pager, next"
                    :current-page="query.pageIndex"
                    :page-size="query.pageSize"
                    :total="pageTotal"
                    @current-change="handlePageChange"
                ></el-pagination>
            </div>
        </div>

        <!-- // 查看弹出框 -->
        <el-dialog title="查看" :visible.sync="checkVisible" width="50%">
            <el-form ref="lookForm" :model="lookForm" label-width="98px">
                <el-form-item label="公司名称">
                    <el-input v-model="lookForm.companyName" disabled></el-input>
                </el-form-item>
                <el-form-item label="公司LOGO">
                    <el-upload class="avatar-uploader" :data="companyLogo" disabled>
                        <img v-if="lookForm.companyLogo" :src="$myUrl.IMGURL + lookForm.companyLogo" class="avatar" />
                        <i v-else class="el-icon-plus avatar-uploader-icon"></i>
                    </el-upload>
                </el-form-item>

                <el-form-item label="产品视频标题">
                    <el-upload class="avatar-uploader" disabled>
                        <!-- form.qrCode == 产品视频标题 二维码改的懒得重名名 -->
                        <video
                            v-if="lookForm.companyProductVCS"
                            :src="$myUrl.IMGURL + lookForm.companyProductVCS"
                            class="avatar"
                            controls
                        />
                        <i v-else class="el-icon-plus avatar-uploader-icon"></i>
                    </el-upload>
                </el-form-item>
                <el-form-item label="公司简介">
                    <el-input type="textarea" rows="5" v-model="lookForm.companyIntroduce" style="width: 500px" disabled></el-input>
                </el-form-item>
                <el-form-item label="公司详细介绍">
                    <el-input type="textarea" rows="5" v-model="lookForm.companyDetailIntro" disabled></el-input>
                </el-form-item>
                <el-form-item label="公司介绍图">
                    <el-upload class="avatar-uploader" disabled>
                        <img v-if="lookForm.companyIntroduceImg" :src="$myUrl.IMGURL + lookForm.companyIntroduceImg" class="avatar" />
                        <i v-else class="el-icon-plus avatar-uploader-icon"></i>
                    </el-upload>
                </el-form-item>
                <el-form-item label="公司实景图">
                    <el-upload class="avatar-uploader" disabled>
                        <img v-if="lookForm.companyPictureImg" :src="$myUrl.IMGURL + lookForm.companyPictureImg" class="avatar" />
                        <i v-else class="el-icon-plus avatar-uploader-icon"></i>
                    </el-upload>
                </el-form-item>
                <el-form-item label="公司视频">
                    <el-upload class="avatar-uploader" disabled>
                        <!-- <img v-if="form.companyIntroduceVideo" :src="form.companyIntroduceVideo" class="avatar" /> -->
                        <video
                            controls
                            v-if="lookForm.companyIntroduceVideo"
                            :src="$myUrl.IMGURL + lookForm.companyIntroduceVideo"
                            class="avatar"
                        ></video>
                        <i v-else class="el-icon-plus avatar-uploader-icon"></i>
                    </el-upload>
                </el-form-item>
                <el-form-item label="产品宣传视频">
                    <el-upload class="avatar-uploader" disabled>
                        <video
                            controls
                            v-if="lookForm.companyProductVideo"
                            :src="$myUrl.IMGURL + lookForm.companyProductVideo"
                            class="avatar"
                        ></video>
                        <!-- <img v-if="form.companyProductVideo" :src="form.companyProductVideo" class="avatar" /> -->
                        <i v-else class="el-icon-plus avatar-uploader-icon"></i>
                    </el-upload>
                </el-form-item>

                <el-form-item>
                    <el-button @click="checkVisible = false">取消</el-button>
                </el-form-item>
            </el-form>
        </el-dialog>
        <!-- 编辑弹出框 -->
        <el-dialog title="编辑" :visible.sync="editVisible" width="30%">
            <el-form ref="form" :model="form" label-width="70px">
                <el-form-item label="用户名">
                    <el-input v-model="form.name"></el-input>
                </el-form-item>
                <el-form-item label="地址">
                    <el-input v-model="form.address"></el-input>
                </el-form-item>
            </el-form>
            <span slot="footer" class="dialog-footer">
                <el-button @click="editVisible = false">取 消</el-button>
                <el-button type="primary" @click="saveEdit(form)">确 定</el-button>
            </span>
        </el-dialog>
    </div>
</template>

<script>
import axios from 'axios';
//import { fetchData } from '../../api/index';
//import {getCompanyList,deleteCompany} from '../../api/company'
export default {
    name: 'basetable',
    data() {
        return {
            query: {
                address: '',
                name: '',
                pageIndex: 1,
                pageSize: 10
            },
            tableData: [],
            multipleSelection: [],
            delList: [],
            editVisible: false,
            checkVisible: false,
            pageTotal: 0,
            form: {},
            checkForm: {},
            lookForm: {},
            idx: -1,
            id: -1,
            labelposition: 'left'
        };
    },
    mounted() {
        // this.getData();
        this._getCompanyList();
        //this.ideovSource = this.ideovUrl + this.$route.params.fileId + "/output.m3u8"
    },
    methods: {
        _getCompanyList() {
            axios.get(this.$myUrl.BASEURL + 'company/getCompanyList?pageNum=0&pageSize=0').then((res) => {
                ////console.log(res);
                this.tableData = res.data.data;
                this.tableData.forEach((item, index) => {
                    ////console.log(item)
                    item.companyLogo = this.$myUrl.IMGURL + item.companyLogo;
                });
                this.pageTotal = this.tableData.length;
            });
        },
        addCompany() {
            this.$router.push({
                path: '/company-detail'
            });
        },
        addBaseTable() {
            this.$router.push({
                path: `/BaseTableAdd?isCompany=0`
            });
        },
        toDetail(index, row) {
            console.log('-----------', row);
            this.$router.push({
                path: '/product-list',
                query: {
                    companyId: row.companyId
                }
            });
            // axios.put(this.$myUrl.BASEURL + 'company/generateCompanyQrCode?companyId='+id.companyId).then(res=>{
            //     //console.log(res)
            // })
        },

        toCheck(index, id) {
            //console.log(index+'------'+id)
            this.checkVisible = true;
            const that = this;
            axios.get(this.$myUrl.BASEURL + 'company/getCompany?companyId=' + id).then((res) => {
                console.log(res);
                this.lookForm = res.data.data;
            });
        },
        // 获取 easy-mock 的模拟数据
        getData() {
            ////console.log(this.tableData);
            // this.tableData.forEach((item,index)=>{
            //         ////console.log(item)
            //         if(this.query.name==item.companyName){

            //             axios.get(this.$myUrl.BASEURL + 'company/getCompany?companyId='+item.companyId).then((res) => {

            //                 this.tableData =[]
            //                 ////console.log(res);
            //                 this.tableData[this.tableData.length] = {'companyId':res.data.data.id,'companyName':res.data.data.companyName,'companyIntroduce':res.data.data.companyIntroduce,'companyLogo':this.$myUrl.BASEURL+res.data.data.companyLogo,'createTime':res.data.data.createTime}
            //                 ////console.log(this.tableData)
            //                 this.pageTotal = res.pageTotal || 50;
            //             });
            //         }

            //     })
            axios
                .get(this.$myUrl.BASEURL + 'company/getCompanyList?companyName=' + this.query.name + '&pageNum=0&pageSize=0')
                .then((res) => {
                    //console.log(res);
                    this.tableData = [];

                    this.tableData = res.data.data;
                    this.tableData.forEach((item, index) => {
                        item.companyLogo = this.$myUrl.IMGURL + item.companyLogo;
                    });
                });
        },
        // 触发搜索按钮
        handleSearch() {
            this.query.pageIndex = 1
            this.$set(this.query, 'pageIndex', 1);
            this.getData();
        },
        // 删除操作
        handleDelete(index, id) {
            // 二次确认删除
            this.$confirm('确定要删除吗？', '提示', {
                type: 'warning'
            })
                .then(() => {
                    axios.delete(this.$myUrl.BASEURL + 'company/deleteCompany?companyId=' + id).then((res) => {
                        //console.log(res)
                    });
                    this.$message.success('删除成功');
                    this.tableData.splice(index, 1);
                })
                .catch(() => {});
        },
        // 多选操作
        handleSelectionChange(val) {
            this.multipleSelection = val;
        },
        delAllSelection() {
            const length = this.multipleSelection.length;
            let str = '';
            this.delList = this.delList.concat(this.multipleSelection);
            for (let i = 0; i < length; i++) {
                str += this.multipleSelection[i].name + ' ';
            }
            this.$message.error(`删除了${str}`);
            this.multipleSelection = [];
        },
        // 编辑操作
        handleEdit(index, row) {
            ////console.log(index)
            console.log(row);
            // 清空列表
            this.$router.push(`company-detail?companyId=${row.companyId}`);
            this.idx = index;
            this.form = row;
            // this.editVisible = true;
            //console.log(this.form)
        },
        // 保存编辑
        saveEdit() {
            //console.log(this.form)
            this.form.companyName = this.form.name;
            this.form.companyIntroduce = this.form.address;
            this.editVisible = false;
            this.$message.success(`修改第 ${this.idx + 1} 行成功`);
            this.$set(this.tableData, this.idx, this.form);
            axios
                .put(this.$myUrl.BASEURL + 'company/updateCompany', {
                    id: this.form.companyId,
                    companyName: this.form.companyName,
                    companyLogo: this.form.companyLogo,
                    companyIntroduce: this.form.companyIntroduce
                })
                .then((res) => {
                    //console.log(res)
                });
        },
        // 分页导航
        handlePageChange(val) {
            this.$set(this.query, 'pageIndex', val);
            this._getCompanyList();
        }
    }
};
</script>

<style scoped>
.handle-box {
    margin-bottom: 20px;
}

.handle-select {
    width: 120px;
}

.handle-input {
    width: 300px;
    display: inline-block;
}
.table {
    width: 100%;
    font-size: 14px;
}
.red {
    color: #ff0000;
}
.mr10 {
    margin-right: 10px;
}
.table-td-thumb {
    display: block;
    margin: auto;
    /* width: 100px;
    height: 80px; */
    /* border-radius: 50%; */
}
.check_image {
    width: 100px;
    height: 70px;
}
.avatar-uploader video {
   width: 100%;
}
.avatar-uploader img  {
   width: 100%;
}
</style>
